import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoadingUtil {
  // 私有构造函数
  LoadingUtil._();

  /// 初始化配置
  static void initialize({EasyLoadingOptions? options}) {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.dark
      ..indicatorSize = 45.0
      ..radius = 10.0
      ..progressColor = Colors.yellow
      ..backgroundColor = Colors.green
      ..indicatorColor = Colors.yellow
      ..textColor = Colors.yellow
      ..maskColor = Colors.blue.withOpacity(0.5)
      ..userInteractions = true
      ..dismissOnTap = false
      ..customAnimation = CustomAnimation();

    // 应用自定义配置
    if (options != null) {
      _applyCustomOptions(options);
    }
  }

  /// 显示加载指示器
  static Future<void> show({
    String? status,
    EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
    bool dismissOnTap = false,
  }) async {
    await EasyLoading.show(
      status: status,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// 显示成功提示
  static Future<void> success(
      String status, {
        Duration? duration,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
        bool dismissOnTap = false,
      }) async {
    await EasyLoading.showSuccess(
      status,
      duration: duration,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// 显示错误提示
  static Future<void> error(
      String status, {
        Duration? duration,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
        bool dismissOnTap = false,
      }) async {
    await EasyLoading.showError(
      status,
      duration: duration,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// 显示信息提示
  static Future<void> info(
      String status, {
        Duration? duration,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
        bool dismissOnTap = false,
      }) async {
    await EasyLoading.showInfo(
      status,
      duration: duration,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// 显示进度指示器
  static Future<void> showProgress(
      double value, {
        String? status,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
      }) async {
    await EasyLoading.showProgress(
      value,
      status: status,
      maskType: maskType,
    );
  }

  /// 显示Toast提示
  static Future<void> showToast(
      String status, {
        Duration? duration,
        EasyLoadingToastPosition toastPosition = EasyLoadingToastPosition.bottom,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.none,
        bool dismissOnTap = false,
      }) async {
    await EasyLoading.showToast(
      status,
      duration: duration,
      toastPosition: toastPosition,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// 隐藏加载指示器
  static Future<void> dismiss({bool animation = true}) async {
    await EasyLoading.dismiss(animation: animation);
  }

  /// 检查是否正在显示
  static bool isShow() {
    return EasyLoading.isShow;
  }

  /// 应用自定义配置
  static void _applyCustomOptions(EasyLoadingOptions options) {
    if (options.indicatorType != null) {
      EasyLoading.instance.indicatorType = options.indicatorType!;
    }
    if (options.loadingStyle != null) {
      EasyLoading.instance.loadingStyle = options.loadingStyle!;
    }
    if (options.indicatorSize != null) {
      EasyLoading.instance.indicatorSize = options.indicatorSize!;
    }
    if (options.radius != null) {
      EasyLoading.instance.radius = options.radius!;
    }
    if (options.progressColor != null) {
      EasyLoading.instance.progressColor = options.progressColor!;
    }
    if (options.backgroundColor != null) {
      EasyLoading.instance.backgroundColor = options.backgroundColor!;
    }
    if (options.indicatorColor != null) {
      EasyLoading.instance.indicatorColor = options.indicatorColor!;
    }
    if (options.textColor != null) {
      EasyLoading.instance.textColor = options.textColor!;
    }
    if (options.maskColor != null) {
      EasyLoading.instance.maskColor = options.maskColor!;
    }
    if (options.userInteractions != null) {
      EasyLoading.instance.userInteractions = options.userInteractions!;
    }
    if (options.dismissOnTap != null) {
      EasyLoading.instance.dismissOnTap = options.dismissOnTap!;
    }
  }
}

/// 自定义配置类
class EasyLoadingOptions {
  final EasyLoadingIndicatorType? indicatorType;
  final EasyLoadingStyle? loadingStyle;
  final double? indicatorSize;
  final double? radius;
  final Color? progressColor;
  final Color? backgroundColor;
  final Color? indicatorColor;
  final Color? textColor;
  final Color? maskColor;
  final bool? userInteractions;
  final bool? dismissOnTap;

  EasyLoadingOptions({
    this.indicatorType,
    this.loadingStyle,
    this.indicatorSize,
    this.radius,
    this.progressColor,
    this.backgroundColor,
    this.indicatorColor,
    this.textColor,
    this.maskColor,
    this.userInteractions,
    this.dismissOnTap,
  });
}

/// 自定义动画
class CustomAnimation extends EasyLoadingAnimation {
  CustomAnimation();

  @override
  Widget buildWidget(
      Widget child,
      AnimationController controller,
      AlignmentGeometry alignment,
      ) {
    return Opacity(
      opacity: controller.value,
      child: child,
    );
  }
}